﻿#include "FITKGraphObjectHighlightBase.h"

// VTK
#include <vtkProperty.h>

// Global data
#include "FITK_Kernel/FITKCore/FITKDataRepo.h"

// Graph
#include "FITK_Interface/FITKVTKAlgorithm/FITKGraphActor.h"
#include "FITKGraphCommons.h"

namespace Exchange
{
    FITKGraphObjectHighlightBase::FITKGraphObjectHighlightBase(AbaqusData::FITKDataCase* caseData, Core::FITKAbstractDataObject* dataObj)
        : FITKGraphObject3D(caseData, dataObj)
    {
        // Set the layer need to be rendered.
        setRenderLayer(1);

        // Save the graph information.
        m_graphInfo.Type = OtherGraph;
    }   

    void FITKGraphObjectHighlightBase::initActorProperties(vtkProp* actor, QVariant details)
    {
        if (!actor)
        {
            return;
        }

        // Set model actor properties.
        // Default show face and edge.
        FITKGraphActor* fActor = FITKGraphActor::SafeDownCast(actor);
        if (!fActor)
        {
            return;
        }

        // Red wireframe or point.
        fActor->setScalarVisibility(false);
        fActor->SetPickable(false);

        fActor->GetProperty()->SetColor(1., 0., 0.);
        fActor->GetProperty()->SetEdgeColor(1., 0., 0.);

        if (details.toInt() == 1)
        {
            // Points.
            fActor->GetProperty()->SetRepresentation(0);
            fActor->GetProperty()->SetPointSize(FITKGraphCommons::s_highlightPointSize);
        }
        else // if (details.toInt() == 2)
        {
            // Wireframe.
            fActor->GetProperty()->SetRepresentation(1);
            fActor->GetProperty()->SetEdgeVisibility(true);
            fActor->GetProperty()->SetLineWidth(FITKGraphCommons::s_highlightLineWidth);
        }
        //else
        //{
        //    // Other special types.
        //}
    }

    void FITKGraphObjectHighlightBase::setColor(QColor color)
    {
        if (!color.isValid())
        {
            return;
        }

        double color3[3]{ 0., 0., 0. };
        FITKGraphCommons::QColorToDouble3(color, color3);

        // Set all actors color.
        for (vtkProp* prop : m_actorList)
        {
            FITKGraphActor* fActor = FITKGraphActor::SafeDownCast(prop);
            if (fActor)
            {
                fActor->GetProperty()->SetColor(color3);
            }
        }
    }

    void FITKGraphObjectHighlightBase::highlight(QColor color)
    {
        // Highlight with color red default.
        double color3[3]{ 0., 0., 0. };
        if (color.isValid())
        {
            FITKGraphCommons::QColorToDouble3(color, color3);
        }
        else
        {
            FITKGraphCommons::QColorToDouble3(Qt::red, color3);
        }

        // Set all actors color.
        for (vtkProp* prop : m_actorList)
        {
            FITKGraphActor* fActor = FITKGraphActor::SafeDownCast(prop);
            if (fActor)
            {
                fActor->GetProperty()->SetColor(color3);
            }
        }

        this->Superclass::highlight();
    }

    bool FITKGraphObjectHighlightBase::isRelatedTo(int dataObjId)
    {
        // Check if the id is the self-object id.
        bool isTheObj = this->Superclass::isRelatedTo(dataObjId);
        if (isTheObj)
        {
            return true;
        }

        // Check if the model ids contains the data id.
        return m_modelIds.contains(dataObjId);
    }
}   // namespace Exchange